package com.woniu.controller;

import com.woniu.service.OrderService;
import com.woniu.util.JwtUtils;
import com.woniu.vo.OrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
@Api(tags = "订单模块")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private JwtUtils jwtUtils;

    /**
     * 生成订单
     */
    @PostMapping("/createOrder/{addressID}")
    @ApiOperation(value = "生成订单")
    public ResponseEntity<String> createOrder(@RequestHeader(name = "Authorization", required = false) String token,
                                              @PathVariable Integer addressID) {
        // 获取 userId
        String userId = jwtUtils.getValue(token, "userId");
        // 创建订单
        String orderNum = orderService.createOrder(Long.valueOf(userId), addressID);
        return ResponseEntity.ok("订单提交成功，订单号：" + orderNum);
    }

    /**
     * 查询订单
     * */
    @GetMapping("/loadOrdersByStatus")
    @ApiOperation(value = "查询订单")
    public ResponseEntity<List<OrderVo>> loadOrdersbyStatus(@RequestHeader(name = "Authorization", required = false) String token,
                                                            Integer status){
        String userId = jwtUtils.getValue(token, "userId");
        List<OrderVo> orderVos = orderService.queryOrder(Long.valueOf(userId), status);
        return ResponseEntity.ok(orderVos);
    }
}
